package com.xudm.cdg.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author tayler
 * @Date 2024/6/18 10:09
 */
public class JsonUtil {
    public static BufferedImage jsonToImg(String jsonString) {
//        String jsonString = "{\n" +
//                "    url:\"http://www.example.com/ybgs\",\n" +
//                "    viewId:\"12378812737019293\",\n" +
//                "    formulaContent:\"限定内容\",\n" +
//                "    formulaName:\"公式1\"\n" +
//                "}";

        Font font = new Font(null, Font.PLAIN, 12);

        // Measure the text dimensions
        BufferedImage dummyImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = dummyImage.createGraphics();
        g2d.setFont(font);
        FontMetrics metrics = g2d.getFontMetrics();

        // Calculate the width and height required for the image
        String[] lines = jsonString.split("\n");
        int maxWidth = 0;
        for (String line : lines) {
            int lineWidth = metrics.stringWidth(line);
            if (lineWidth > maxWidth) {
                maxWidth = lineWidth;
            }
        }
        int lineHeight = metrics.getHeight();
        int height = lineHeight * lines.length + 20; // Adding some padding
        int width = maxWidth + 20; // Adding some padding

        // Create the image with calculated dimensions
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        g2d = bufferedImage.createGraphics();

        // White background
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, width, height);

        // RGB colors for keys and values
        Color keyColor = new Color(255, 0, 0); // Red
        Color valueColor = new Color(0, 128, 0); // Green
        Color symbolColor = Color.BLACK;

        // Set font
        g2d.setFont(font);
        metrics = g2d.getFontMetrics();

        int x = 10;
        int y = 30;
        boolean isValue = false;
        boolean inQuotes = false;
        boolean isKey = false;

        for (int i = 0; i < jsonString.length(); i++) {
            char c = jsonString.charAt(i);

            if (c == '{' || c == '}' || c == '\n') {
                g2d.setColor(symbolColor);
                g2d.drawString(String.valueOf(c), x, y);
                x += metrics.charWidth(c);
                if (c == '\n') {
                    x = 10;
                    y += metrics.getHeight();
                }
                isValue = false;
                isKey = false;
            } else if (c == ':') {
                g2d.setColor(symbolColor);
                g2d.drawString(String.valueOf(c), x, y);
                x += metrics.charWidth(c);
                isValue = true;
                isKey = false;
            } else if (c == '"') {
                inQuotes = !inQuotes;
                if (inQuotes) {
                    isKey = !isValue;
                }
                g2d.setColor(isKey ? keyColor : valueColor);
                g2d.drawString(String.valueOf(c), x, y);
                x += metrics.charWidth(c);
            } else {
                g2d.setColor(inQuotes ? (isKey ? keyColor : valueColor) : symbolColor);
                g2d.drawString(String.valueOf(c), x, y);
                x += metrics.charWidth(c);
            }
        }

        g2d.dispose();
//        FileInputStream fis;
//        try {
//            ByteArrayOutputStream baos = new ByteArrayOutputStream();
//            ImageIO.write(bufferedImage, "png", baos);
//            baos.flush();
//            byte[] imageBytes = baos.toByteArray();
//            baos.close();
//
//            // Write byte array to a temporary file
//            File tempFile = File.createTempFile("json_output", ".png");
//            FileOutputStream fos = new FileOutputStream(tempFile);
//            fos.write(imageBytes);
//            fos.close();
//
//            // Create FileInputStream from the temporary file
//            fis = new FileInputStream(tempFile);
//
//            // Use the FileInputStream as needed
//            // For example, print out the file path
////            System.out.println("Temporary file created at: " + tempFile.getAbsolutePath());
//
//            // Remember to delete the temporary file when done
//            tempFile.deleteOnExit();
//
//        } catch (IOException e) {
//            System.out.println("文件生成失败");
//            return null;
//        }
//        return fis;
        return bufferedImage;
    }
}
